import timePlugIn from "../../../ipha_tool/TimePlugIn";
import UIService from "../../services/uiService";
import UserService from "../../services/userService";
import { mgrs, sers } from "../../system/OverallSystem ";
import { GAME_MODE } from "../game/gameMode_Json";
import PageBase from "./pageBase";


const { ccclass, property } = cc._decorator;

@ccclass
export default class OverPage extends PageBase {

    public pageName: string = "失败页";

    // @property({ type: cc.Label, displayName: "是否过关文本" })
    // private message: cc.Label = null;

    @property({ type: cc.Node, displayName: "结束页1" })
    private content1: cc.Node = null;

    @property({ type: cc.Node, displayName: "结束页2" })
    private content2: cc.Node = null;


    @property(cc.Label)
    private number1: cc.Label = null

    @property(cc.Label)
    private number2: cc.Label = null


    @property(cc.Node)
    private restart: cc.Node = null

    @property(cc.Node)
    private nextBtn: cc.Node = null

    @property(cc.Node)
    private zhanshiNode: cc.Node = null

    @property(cc.Label)
    private tongguanshijian:cc.Label = null

    @property(cc.RichText)
    private titleLabel:cc.RichText = null

    @property(cc.Node)
    lossNode:cc.Node = null

    @property(cc.Node)
    passTitle:cc.Node = null

    @property(cc.Node)
    lossTitle:cc.Node = null


    @property(cc.Node)
    type2Node = null
    // @property({ type: cc.Node })
    // private xingxingList: cc.Node = null;

    setCloseDoFun(colseDoFun: Function) {
        this.colseDoFun = colseDoFun;
    }

    start() {

        this._init();
       
        sers().platformSrv.platformApi.recordedGameScreenStop()
    }

    protected _init() {
        this.open();

    }

    private moshi = 1
    initData(data) {
        cc.log(data)
        if(data.data.jinduNode && !data.pass){
            cc.log('zhanshi')
            let jinduNode = data.data.jinduNode
           
            jinduNode.getComponent(cc.Widget).enabled = false
            jinduNode.y = 0

            // jinduNode.parent = this.lossNode
            let level = data.data.level

            this.lossNode.active = true

            let dalevel = 1
            if (level > 2) {
                dalevel = 2
            }
            if (level > 7) {
                dalevel = 3
            }
            if(this.lossNode.getChildByName('jindu'+dalevel)){
                this.lossNode.getChildByName('jindu'+dalevel).active = true
                let node = this.lossNode.getChildByName('jindu'+dalevel)
               for (const iterator of node.children) {
                    if(+iterator.name <= +level){
                        iterator.getChildByName('gou').active = true
                    }
               }
            }
            
            this.zhanshiNode.active = false
            this.passTitle.active = false
            this.lossTitle.active = true
            // this.tongguanshijian.string = timePlugIn().numberShiftTime(data.data.time)
            this.restart.active = true;
            this.nextBtn.active = false;
            let UserData = sers().userSrv.getUserData()
            let l = sers().userSrv.getStoreMajiang2Data()

            if(UserData.city){
                this.titleLabel.string = `<color=#FFFFFF>你所在的地区已有</c><color=#BE3F41>${UserData.city.university}</color>人晋升雀神
                <color=#FFFFFF>再提升</c><color=#BE3F41>${11-l.level}</color>个等级即可通关！`
            }else{
                this.titleLabel.string = `<color=#FFFFFF>再提升</c><color=#BE3F41>${11-l.level}</color>个等级即可通关！`
            }

           
          
            // let text = ['简单','困难','极难']
            // this.titleLabel.string = `<color=#FFFFFF>恭喜通过</c><color=#0fffff>${text[data.data.level-1]}</color>阶段`
        }

        if(data.pass){
            this.lossNode.active =false
            this.zhanshiNode.active = true
            this.passTitle.active = true
            this.lossTitle.active = false
            this.restart.active = false
            this.nextBtn.active = true

            let UserData = sers().userSrv.getUserData()
            let l = sers().userSrv.getStoreMajiang2Data()
            this.titleLabel.string = `<color=#FFFFFF>你所在的地区已有</c><color=#BE3F41>${UserData.city.university}</color>人晋升雀神`
            if(11-l.level==10){
                this.titleLabel.string = `<color=#FFFFFF>你所在的地区已有</c><color=#BE3F41>${UserData.city.university}</color>人晋升雀神
                <color=#FFFFFF>恭喜通关！成为雀神！</c>`
            }

            if(UserData.city){
                this.titleLabel.string = `<color=#FFFFFF>你所在的地区已有</c><color=#BE3F41>${UserData.city.university}</color>人晋升雀神`
            }else{
                this.titleLabel.string = `<color=#FFFFFF>你暂未选择地区</c>`
                if(11-l.level==10){
                    this.titleLabel.string = `<color=#FFFFFF>恭喜通关！成为雀神！</c>`
                }
    
            }
        }
        
        if(data.data.modeType==2){
            this.titleLabel.node.parent.active = false
            this.lossNode.active = false
            if(!data.pass){
                this.type2Node.active = true
                this.zhanshiNode.active = false
            }
           this.moshi = 2
        }
    }

  

    share() {
        // sers().platformSrv.platformApi.shareRecordedGameScreen()
        sers().platformSrv.platformApi.shareAppMessage('','据说只有智商爆表的人才能晋升雀神，你也来试试吧。')
    }

    newGame() {
        // this.OnBtnClose()
        mgrs().soundMgr.playBtnSound();
        // sers().userSrv.enterModeDeductPower(1, () => {
        // sers().platformSrv.platformApi.hideBanner()
        // mgrs().gameMgr.enterMode(GAME_MODE.NORMAL);

        if(this.colseDoFun){
            this.colseDoFun()
            this.close()
        }else{
            this.backIndex()
        }
        // }, () => {
        //     sers().uiSrv.showMessage("体力不足");
        //     sers().uiSrv.showUserGetPowerPage();
        // })
    }

    restartGame() {

        sers().platformSrv.platformApi.hideBanner()
        // let shuju = {
        //     level:1
        // }
        // sers().userSrv.setStoreMajiang2Data(shuju)
        if(this.moshi==2){
            mgrs().gameMgr.enterMode(GAME_MODE.MAJIANGLEVEL);
        }else{
            mgrs().gameMgr.enterMode(GAME_MODE.MAJIANG);
        }

    }

    open() {
        UIService.openPageBasicAni(this.showNode, true)
            .then(() => {

            }).catch(() => {

            });
    }

    close() {
        sers().platformSrv.platformApi.hideBanner()

        this.closeAni()
            .then(() => {
                // this.colseDoFun && this.colseDoFun();

                sers().uiSrv.deletePageRecord(this.id);

                this.node.destroy();
      
            })
            .catch();
    }

    onlyClose() {
        this.closeAni()
            .then(() => {
                sers().uiSrv.deletePageRecord(this.id);
                this.node.destroy();
            })
            .catch();
    }

    OnBtnClose() {
        mgrs().soundMgr.playBtnSound();
        mgrs().gameMgr.backHomePage();
        this.close();
        cc.log("OnBtnClose");
    }

    backIndex() {
        this.OnBtnClose()
    }

    protected closeAni(isResume: boolean = false) {
        return UIService.closePageBasicAni(this.showNode, isResume);
    }

    update(dt) {

    }
}
